#!/usr/bin/python
#coding=utf-8

from app.lib import BaseHandler
from utils.common import auth
from utils.common import set_navbar 
from utils.common import get_token 
from utils.common import check_token 
from utils.common import render 
from utils.common import pager 
from model.product import Product
from model.product import ProductManager
from utils.model import unquote 
from web.session import Session
from igor import logging


class ItemList(BaseHandler):

    def get(self, req, resp):  
        str_page_no = req.args.get('pno', ['1'])[0]
        pmanager = ProductManager()
        count = pmanager.count()
        PAGE_SIZE = 8 
        page_no, total_pages, prev, next = pager(str_page_no, count, PAGE_SIZE)
        start, limit = (page_no-1)*PAGE_SIZE, PAGE_SIZE
        items = pmanager.query_page(start, limit)
        items_patterns = {'items': items, 'cur':page_no,
                'first':1, 'prev':prev, 'next':next,
                'last':total_pages, 'count':count}
        patterns = set_navbar(req)
        patterns.update(items_patterns)
        body = render('itemlist.html', patterns) 
        return resp.respond(body)


class ItemDetail(BaseHandler):

    def get(self, req, resp):
        pid = req.args.get('pid', [''])[0]
        msg = '商品不存在'
        if pid == '':
            return resp.respond(msg)
        pmanager = ProductManager()
        item = pmanager.query_by_id(pid)
        if not item:
            return resp.respond(msg)
        patterns = set_navbar(req)
        patterns.update({'item': item })
        body = render('itemdetail.html', patterns)
        return resp.respond(body) 


class ShowImg(BaseHandler):

    def get(self, req, resp):
        pid = req.args.get('pid', [''])[0]
        pmanager = ProductManager()
        img = None
        if pid != '': 
            img = pmanager.query_img(pid)
        if not img:
            img = open('static/img/default.jpg', 'r').read()
        resp.content_type = 'image/jpeg'
        return resp.respond(img)


class AddItem(BaseHandler):

    @auth
    def get(self, req, resp):
        patterns = set_navbar(req)
        token = get_token(req)
        patterns.update({'token':token})
        body = render('add.html', patterns)
        return resp.respond(body)

    @auth
    @check_token
    def post(self, req, resp):
        args = req.args
        name = args.get('name', [''])[0]
        price = args.get('price', [''])[0]
        taglist = args.get('tags', [''])
        descpt = args.get('descpt', [''])[0]
        pic = args.get('pic', [''])[0]
        session = Session(req)
        uid = session['user'].uid
        pmanager = ProductManager()
        product = Product()
        product.tags = []
        product.name = unquote(name)
        product.price = price
        for tag in taglist:
            if tag != '':
                product.tags.append(unquote(tag))
        product.descpt = unquote(descpt)
        product.img = pic
        product.uid = uid
        logging.log('name=%s, price=%s, tags=%s, descpt=%s, uid=%s' %(
            product.name, price, product.tags, product.descpt, uid))
        pmanager.insert(product)
        return resp.redirect('/account/sell')
